package com.deyuanyun.pic.pipe.service.impl;

import com.deyuanyun.pic.cache.SessionCacheSupport2;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.ConvertUtil;
import com.deyuanyun.pic.common.util.MyCollectionUtil;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.controller.dto.UserSessionVO;
import com.deyuanyun.pic.pipe.domain.PicPipePiggingstructure;
import com.deyuanyun.pic.pipe.domain.PicPipePiggingstructureG;
import com.deyuanyun.pic.pipe.domain.PicPipePiggingstructureGExample;
import com.deyuanyun.pic.pipe.domain.custom.CustomPicPipePiggingstructure;
import com.deyuanyun.pic.pipe.domain.exceldomain.PiggingStructureVO;
import com.deyuanyun.pic.pipe.mapper.CustomPicPipePiggingstructureMapper;
import com.deyuanyun.pic.pipe.mapper.PicPipePiggingstructureGMapper;
import com.deyuanyun.pic.pipe.mapper.PicPipePiggingstructureMapper;
import com.deyuanyun.pic.pipe.service.PipePiggingStructureService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 2016/1/29.
 * 收发球筒
 */
@Service
public class PipePiggingStructureServiceImpl implements PipePiggingStructureService {
    Logger logger=Logger.getLogger(PipePiggingStructureServiceImpl.class);
    @Autowired
    private CustomPicPipePiggingstructureMapper customPicPipePiggingstructureMapper;
    @Autowired
    private PicPipePiggingstructureGMapper piggingstructureGMapper;
    @Autowired
    private PicPipePiggingstructureMapper piggingstructureMapper;

    @Override
    public void insertPiggingstructureList(List<PiggingStructureVO> piggingStructureVOList)throws Exception{
        List<PicPipePiggingstructure> identifications= ConvertUtil.transform(piggingStructureVOList,PicPipePiggingstructure.class);
        Date crDaye=new Date();
        for (PicPipePiggingstructure iden:identifications){
            String id= iden.getId();
            if (StringUtils.isBlank(id)){
                iden.setId(CommonUtil.getUUID());
            }
            iden.setIsNewest((short)0);
            iden.setDeleted((short)0);
            iden.setCreateDate(crDaye);
            iden.setDataStated("20");
            UserSessionVO userSessionVO= SessionCacheSupport2.getSessionVO();
            iden.setCreateUser(userSessionVO.getUsername());

            Map map=new HashMap();
            map.put("priUuid",iden.getId());
            List<PiggingStructureVO> importVOs= MyCollectionUtil.filterListByMap(piggingStructureVOList,map);
            Asserts.assertTrue(ObjectUtil.isNotNullEmpty(importVOs),"导入出错了，没有找到对应的坐标记录");
            PiggingStructureVO importVO=importVOs.get(0);
            PicPipePiggingstructureG g=new PicPipePiggingstructureG();
            g.setDeleted((short)0);
            g.setIsNewest((short)0);
            g.setDataStated("20");
            g.setX(new BigDecimal(importVO.getLaocationX()));
            g.setY(new BigDecimal(importVO.getLaocationY()));
            g.setZ(new BigDecimal(importVO.getLaocationZ()));
            g.setRecorder(iden.getRecorder());
            g.setRecordtime(iden.getRecordtime());
            g.setPipePiggingstructureId(iden.getId());
            g.setId(CommonUtil.getUUID());
            g.setDataStated("20");
            piggingstructureGMapper.insert(g);
            iden.setDataStated("20");
            piggingstructureMapper.insertSelective(iden);
        }
    }

    @Override
    public void delPiggingstructureRecord(String id){
        int i= piggingstructureMapper.deleteByPrimaryKey(id);
        Asserts.assertTrue(i>0,"该记录已经不存在");

        PicPipePiggingstructureGExample gExample=new PicPipePiggingstructureGExample();
        gExample.createCriteria().andPipePiggingstructureIdEqualTo(id);
        piggingstructureGMapper.deleteByExample(gExample);
    }

    @Override
    public List<CustomPicPipePiggingstructure> queryPiggingstructureList(Map map){
        List<CustomPicPipePiggingstructure> piggingstructureList = customPicPipePiggingstructureMapper.queryPiggingstructure(map);
        return piggingstructureList;
    }
}
